<?php

class Mailer
{


	/**
	 * @var string  <CR><LF>.
	 */
	const CRLF = "\r\n";

	/**
	 * @var  array Website settings
	 **/
	protected $settings = array();


	private $attachment = false;
	public $to;
	public $from;
	public $subject;
	public $message;
	private $sep;
	/**
	 * Consrtuctor.
	 *
	 * @return  Auth
	 */
	public function __construct($to = null, $subject = null, $message = null, $from = null)
	{
		$this->to = $to;
		$this->from = $from;
		$this->subject = $subject;
		$this->message = $message;
		$this->init();
	}

	private function init()
	{
		$settings = Database::instance()->query('SELECT * FROM `settings`')->fetchAll();
		$values = array();
		foreach ($settings as $setting) {
			$this->settings[$setting['key']] = $values[$setting['key']] = $setting['value'];
		}
		unset($settings);
		$this->db = Database::instance();
		$this->boundary();
	}

	public function boundary()
	{
		$this->sep = sha1(date('r', time()));
	}

	public function attachment($file)
	{
		try {
			$attachment = file_get_contents($file);
		} catch (Exception $e) {
		}
		$this->attachment .= self::CRLF. '--PHP-mixed-' . $this->sep . self::CRLF;
		$this->attachment .= 'Content-Type: ' . mime_content_type($file) . '; name="' . substr($file, strripos($file, '/') + 1) . '"' . self::CRLF;
		$this->attachment .= 'Content-Transfer-Encoding: base64' . self::CRLF;
		$this->attachment .= 'Content-Disposition: attachment' . self::CRLF;
		$this->attachment .= self::CRLF;
		$this->attachment .=  chunk_split(base64_encode($attachment));
		$this->attachment .= self::CRLF;
	}

	public function message($message)
	{
		if (is_array($message)) {
			$htmlMessage = isset($message['html']) ? $message['html'] : (isset($message['text']) ? $message['text'] : '');
			$textMessage = isset($message['text']) ? $message['text'] : (isset($message['html']) ? strip_tags(str_ireplace('<br>', self::CRLF, $message['html'])) : '');
			$textMessage = trim(preg_replace("/[\r\n]+/m","\r\n", $textMessage));
			$this->message = '--PHP-mixed-' . $this->sep . self::CRLF;
			$this->message .= 'Content-Type: multipart/alternative; boundary="PHP-alt-' . $this->sep . '"' . self::CRLF;
			$this->message .= self::CRLF;
			$this->message .= '--PHP-alt-' . $this->sep . self::CRLF;
			$this->message .= 'Content-Type: text/plain; charset=UTF-8' . self::CRLF;
			$this->message .= self::CRLF;
			$this->message .= $textMessage . self::CRLF;
			$this->message .= self::CRLF;
			$this->message .= '--PHP-alt-' . $this->sep . self::CRLF;
			$this->message .= 'Content-Type: text/html; charset=UTF-8' . self::CRLF;
			$this->message .= self::CRLF;
			$this->message .= $htmlMessage . self::CRLF;
			$this->message .= self::CRLF;
			$this->message .= '--PHP-alt-' . $this->sep . '--' . self::CRLF . self::CRLF;
		} else {
			$this->message = '--PHP-alt-' . $this->sep . self::CRLF;
			$this->message .= 'Content-Type: text/plain; charset=UTF-8' . self::CRLF;
			$this->message .= self::CRLF;
			$this->message .= $message . self::CRLF;
			$this->message .= self::CRLF;
			$this->message .= '--PHP-alt-' . $this->sep . self::CRLF . self::CRLF;
			$this->message .= 'Content-Type: text/html; charset=UTF-8' . self::CRLF;
			$this->message .= self::CRLF;
			$this->message .= $message . self::CRLF;
			$this->message .= self::CRLF;
			$this->message .= '--PHP-alt-' . $this->sep . '--' . self::CRLF . self::CRLF;
		}
	}

	public function send()
	{
		$headers = 'MIME-Version: 1.0' . self::CRLF;
		$headers .= 'Content-Type: multipart/mixed; boundary="PHP-mixed-' . $this->sep . '"' . self::CRLF;
		$headers .= 'From: ' . $this->from . self::CRLF;
		$headers .= 'X-Mailer: PHP/' . phpversion() . self::CRLF;
		$message = $this->message . $this->attachment . '--PHP-mixed-' . $this->sep . '--';
		return mail($this->to, $this->subject, $message, $headers);
	}

	public static function sendMail($to, $subj, $message, $from = 'mail@ukietech.com')
	{
		$headers = 'From: ' . $from . "\r\n" . 'X-Mailer: PHP/' . phpversion();
		if (is_array($message)) {
			$htmlMessage = isset($message['html']) ? $message['html'] : (isset($message['text']) ? $message['text'] : '');
			$textMessage = isset($message['text']) ? $message['text'] : (isset($message['html']) ? strip_tags(str_ireplace('<br>', "\r\n", $message['html'])) : '');
			$hash = md5(date('r', time()));
			$headers .= "\r\n" . 'Content-Type: multipart/alternative; boundary="Bound-' . $hash . '"';
			$mes = '--Bound-' . $hash . "\r\n";
			$mes .= 'Content-Type: text/plain; charset="utf-8"' . "\r\n";
			$mes .= 'Content-Transfer-Encoding: 7bit' . "\r\n";
			$mes .= $textMessage . "\r\n";
			$mes .= '--Bound-' . $hash . "\r\n";
			$mes .= 'Content-Type: text/html; charset="utf-8"' . "\r\n";
			$mes .= 'Content-Transfer-Encoding: 7bit' . "\r\n";
			$mes .= $htmlMessage . "\r\n";
			$mes .= '--Bound-' . $hash . "\r\n";
		} else {
			$mes = $message;
		}
		return mail($to, $subj, $mes, $headers);
	}
}